//
//  ChartWeekViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 05.04.25.
//

import Foundation
import SwiftUI

class ChartDateSubViewModel : ChartDateViewModel {
    
    func setCategory(category: AccountingCategory?) -> ChartDateSubViewModel {
        self.category = category
        return self
    }
    
    override func fetchWeekAmountTotal(weekDays: Array<String>, categoryType: CategoryType) -> Double {
        if let parentId = category?.cid {
            return detailDao.getWeekTotalAmount(
                weekDays: weekDays,
                categoryType: categoryType.rawValue,
                uid: self.uid,
                bookId: self.bookId,
                cid: parentId
            )
        } else {
            return super.fetchWeekAmountTotal(weekDays: weekDays, categoryType: categoryType)
        }
    }
    
    override func fetchMonthAmountTotal(month: String, categoryType: CategoryType) -> Double {
        if let parentId = category?.cid {
            return detailDao.getMonthTotalAmount(
                month: month,
                categoryType: categoryType.rawValue,
                uid: self.uid,
                bookId: self.bookId,
                cid: parentId
            )
        } else {
            return super.fetchMonthAmountTotal(month: month, categoryType: categoryType)
        }
    }
    
    override func fetchYearAmountTotal(year: String, categoryType: CategoryType) -> Double {
        if let parentId = category?.cid {
            return detailDao.getYearTotalAmount(
                year: year,
                categoryType: categoryType.rawValue,
                uid: self.uid,
                bookId: self.bookId,
                cid: parentId
            )
        } else {
            return super.fetchYearAmountTotal(year: year, categoryType: categoryType)
        }
    }
    
    override func fetchWeekDayAmount(weekDays: Array<String>, categoryType: CategoryType) -> [String : Double] {
        if let parentId = category?.cid {
            return detailDao.getDayAmountByWeekDays(
                weekDays: weekDays,
                categoryType: categoryType.rawValue,
                uid: self.uid,
                bookId: self.bookId,
                cid: parentId
            )
        } else {
            return super.fetchWeekDayAmount(weekDays: weekDays, categoryType: categoryType)
        }
    }
    
    override func fetchMonthDayAmount(month: String, categoryType: CategoryType) -> [String : Double] {
        if let parentId = category?.cid {
            return detailDao.getDayAmountByMonth(
                month: month,
                categoryType: categoryType.rawValue,
                uid: self.uid,
                bookId: self.bookId,
                cid: parentId
            )
        } else {
            return super.fetchMonthDayAmount(month: month, categoryType: categoryType)
        }
    }
    
    override func fetchYearDayAmount(year: String, categoryType: CategoryType) -> [String : Double] {
        if let parentId = category?.cid {
            return detailDao.getDayAmountByYear(
                year: year,
                categoryType: categoryType.rawValue,
                uid: uid,
                bookId: bookId,
                cid: parentId
            )
        } else {
            return super.fetchYearDayAmount(year: year, categoryType: categoryType)
        }
    }
    
    override func fetchPieDataByWeekDays(weekDays: Array<String>, categoryType: CategoryType) -> [CategoryAmount] {
        if let parentId = category?.cid {
            return detailRepo.getWeekPieDataByCid(weekDays: weekDays, categoryType: categoryType.rawValue, cid: parentId, uid: uid, bookId: bookId)
        } else {
            return super.fetchPieDataByWeekDays(weekDays: weekDays, categoryType: categoryType)
        }
    }
    
    override func fetchPieDataByMonth(month: String, categoryType: CategoryType) -> [CategoryAmount] {
        if let parentId = category?.cid {
            return detailRepo.getMonthPieDataByCid(month: month, categoryType: categoryType.rawValue, cid: parentId, uid: uid, bookId: bookId)
        } else {
            return super.fetchPieDataByMonth(month: month, categoryType: categoryType)
        }
    }
    
    override func fetchPieDataByYear(year: String, categoryType: CategoryType) -> [CategoryAmount] {
        if let parentId = category?.cid {
            return detailRepo.getYearPieDataByCid(year: year, categoryType: categoryType.rawValue, cid: parentId, uid: uid, bookId: bookId)
        } else {
            return super.fetchPieDataByYear(year: year, categoryType: categoryType)
        }
    }
}
